<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>级联选择器与表格联动示例</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
            background: #f5f5f5;
            line-height: 1.6;
        }
        
        .cascader-table-example {
            max-width: 1400px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 12px;
        }

        .header h1 {
            margin: 0 0 10px 0;
            font-size: 2rem;
        }

        .header p {
            margin: 0;
            opacity: 0.9;
        }

        .selector-section,
        .table-section,
        .log-section {
            margin-bottom: 30px;
            padding: 24px;
            background: #f8f9fa;
            border-radius: 12px;
            border-left: 5px solid #667eea;
        }

        .selector-section h2,
        .table-section h2,
        .log-section h2 {
            margin: 0 0 20px 0;
            color: #2c3e50;
            font-size: 1.3rem;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #34495e;
        }

        .selected-info {
            margin-top: 20px;
            padding: 20px;
            background: white;
            border-radius: 8px;
            border: 1px solid #e9ecef;
        }

        .selected-info h3 {
            margin: 0 0 15px 0;
            color: #667eea;
            font-size: 1.1rem;
        }

        .info-grid {
            display: grid;
            gap: 12px;
        }

        .info-item {
            display: flex;
            align-items: center;
        }

        .info-item .label {
            min-width: 120px;
            font-weight: 600;
            color: #6c757d;
        }

        .info-item .value {
            color: #495057;
        }

        .info-item .value.highlight {
            background: #e7f3ff;
            color: #667eea;
            padding: 4px 8px;
            border-radius: 4px;
            font-weight: 600;
            font-family: 'Courier New', monospace;
        }

        .request-info {
            margin-bottom: 24px;
            padding: 20px;
            background: white;
            border-radius: 8px;
            border: 1px solid #e9ecef;
        }

        .request-info h3 {
            margin: 0 0 15px 0;
            color: #667eea;
            font-size: 1.1rem;
        }

        .request-details {
            display: grid;
            gap: 10px;
        }

        .request-item {
            display: flex;
            align-items: flex-start;
            gap: 10px;
        }

        .request-item .label {
            min-width: 100px;
            font-weight: 600;
            color: #6c757d;
            flex-shrink: 0;
        }

        .request-item code {
            background: #f1f3f4;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 13px;
            white-space: pre-wrap;
            flex: 1;
        }

        .status.loading {
            color: #ffc107;
        }

        .status.success {
            color: #28a745;
        }

        .table-container {
            background: white;
            border-radius: 8px;
            overflow: hidden;
            border: 1px solid #e9ecef;
        }

        .data-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 14px;
        }

        .data-table th {
            background: #f8f9fa;
            padding: 12px 16px;
            text-align: left;
            font-weight: 600;
            color: #495057;
            border-bottom: 1px solid #dee2e6;
        }

        .data-table td {
            padding: 12px 16px;
            border-bottom: 1px solid #f1f3f4;
        }

        .data-table tr:hover {
            background-color: #f8f9fa;
        }

        .loading-cell,
        .empty-cell {
            text-align: center;
            color: #6c757d;
            font-style: italic;
            padding: 40px !important;
        }

        .status-tag {
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: 500;
        }

        .status-tag.active {
            background: #d4edda;
            color: #155724;
        }

        .status-tag.inactive {
            background: #f8d7da;
            color: #721c24;
        }

        .log-container {
            max-height: 300px;
            overflow-y: auto;
            background: white;
            border-radius: 8px;
            border: 1px solid #e9ecef;
        }

        .log-item {
            padding: 8px 16px;
            border-bottom: 1px solid #f1f3f4;
            font-size: 13px;
            display: flex;
            gap: 12px;
        }

        .log-time {
            color: #6c757d;
            font-weight: 500;
            min-width: 90px;
        }

        .log-message {
            color: #495057;
        }

        .empty-log {
            padding: 20px;
            text-align: center;
            color: #6c757d;
            font-style: italic;
        }

        /* 级联选择器样式 */
        .cascader-select {
            position: relative;
            width: 100%;
            max-width: 500px;
        }
        
        .input-wrapper {
            position: relative;
            cursor: pointer;
        }
        
        .cascader-input {
            width: 100%;
            padding: 12px 40px 12px 16px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            background-color: #fff;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 14px;
            line-height: 1.5;
            box-sizing: border-box;
        }
        
        .cascader-input:hover {
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .cascader-input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
        }
        
        .arrow-icon {
            position: absolute;
            right: 16px;
            top: 50%;
            transform: translateY(-50%);
            color: #adb5bd;
            font-size: 12px;
            transition: transform 0.3s ease;
            pointer-events: none;
        }
        
        .arrow-icon.arrow-up {
            transform: translateY(-50%) rotate(180deg);
        }
        
        .cascader-dropdown {
            position: absolute;
            top: 100%;
            left: 0;
            right: 0;
            background: white;
            border: 2px solid #e9ecef;
            border-radius: 12px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
            z-index: 2000;
            margin-top: 8px;
            max-height: 450px;
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }
        
        .cascader-content {
            flex: 1;
            overflow: hidden;
        }
        
        .cascader-header {
            display: flex;
            border-bottom: 2px solid #e9ecef;
            background: #fff;
        }
        
        .cascader-header-item {
            flex: 1;
            min-width: 100px;
            width: 100px;
            padding: 8px 12px;
            font-weight: 600;
            font-size: 13px;
            color: #667eea;
            text-align: left;
            border-right: 1px solid #dee2e6;
            background: #fff;
            text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
        }
        
        .cascader-header-item:last-child {
            border-right: none;
        }
        
        .cascader-panels {
            display: flex;
            max-height: 350px;
            overflow: auto;
        }
        
        .cascader-panel {
            flex: 1;
            min-width: 100px;
            width: 100px;
            border-right: 1px solid #e9ecef;
            background: #fff;
        }
        
        .cascader-panel:last-child {
            border-right: none;
        }
        
        .cascader-option {
            padding: 8px 12px;
            cursor: pointer;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 13px;
            color: #495057;
            transition: all 0.2s ease;
            position: relative;
        }
        
        .cascader-option:hover {
            background-color: #f8f9fa;
            color: #667eea;
        }
        
        .cascader-option.is-active {
            color: #667eea;
            background-color: #e7f3ff;
            font-weight: 500;
        }
        
        .cascader-option.is-selected {
            color: #667eea;
            font-weight: 600;
        }
        
        .cascader-option.has-children {
            padding-right: 40px;
        }
        
        .option-label {
            flex: 1;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        
        .option-arrow {
            color: #adb5bd;
            font-size: 12px;
            margin-left: 8px;
            font-weight: bold;
        }
        
        .cascader-footer {
            border-top: 1px solid #e9ecef;
            padding: 16px 20px;
            display: flex;
            justify-content: flex-end;
            gap: 12px;
            background: #fff;
        }
        
        .btn {
            padding: 8px 20px;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            border: 2px solid;
            transition: all 0.3s ease;
        }
        
        .btn-cancel {
            background: #fff;
            color: #6c757d;
            border-color: #dee2e6;
        }
        
        .btn-cancel:hover {
            color: #495057;
            border-color: #adb5bd;
            background-color: #f8f9fa;
        }
        
        .btn-confirm {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: #fff;
            border-color: transparent;
        }
        
        .btn-confirm:hover:not(:disabled) {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
        }
        
        .btn-confirm:disabled {
            background: #adb5bd;
            border-color: #adb5bd;
            cursor: not-allowed;
            transform: none;
        }

        @media (max-width: 768px) {
            .cascader-table-example {
                padding: 10px;
            }
            
            .info-grid {
                grid-template-columns: 1fr;
            }
            
            .info-item {
                flex-direction: column;
                align-items: flex-start;
            }
            
            .info-item .label {
                min-width: auto;
            }
            
            .table-container {
                overflow-x: auto;
            }
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="cascader-table-example">
            <div class="header">
                <h1>🔗 级联选择器与表格联动示例</h1>
                <p>演示如何将级联选择器的值传递给父组件，并作为表格请求参数</p>
            </div>

            <!-- 级联选择器区域 -->
            <div class="selector-section">
                <h2>📋 选择筛选条件</h2>
                <div class="form-group">
                    <label>选择组件分类：</label>
                    <cascader-select
                        :model-value="selectedValue"
                        :options="options"
                        placeholder="请选择分类筛选条件"
                        @update:model-value="updateSelectedValue"
                        @change="handleCascaderChange"
                    ></cascader-select>
                </div>
                
                <!-- 显示当前选中信息 -->
                <div class="selected-info" v-if="selectedValue.length">
                    <h3>🎯 当前选中信息</h3>
                    <div class="info-grid">
                        <div class="info-item">
                            <span class="label">完整路径：</span>
                            <span class="value">{{ selectedLabels.join(' → ') }}</span>
                        </div>
                        <div class="info-item">
                            <span class="label">请求参数值：</span>
                            <span class="value highlight">{{ requestParam }}</span>
                        </div>
                        <div class="info-item">
                            <span class="label">参数说明：</span>
                            <span class="value">使用最深层级的 name 值</span>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 表格区域 -->
            <div class="table-section">
                <h2>📊 数据表格</h2>
                
                <!-- 请求参数显示 -->
                <div class="request-info">
                    <h3>🔧 模拟请求信息</h3>
                    <div class="request-details">
                        <div class="request-item">
                            <span class="label">请求URL：</span>
                            <code>/api/table/data</code>
                        </div>
                        <div class="request-item">
                            <span class="label">请求参数：</span>
                            <code>{{ getRequestParams() }}</code>
                        </div>
                        <div class="request-item">
                            <span class="label">请求状态：</span>
                            <span :class="['status', loading ? 'loading' : 'success']">
                                {{ loading ? '🔄 加载中...' : '✅ 请求成功' }}
                            </span>
                        </div>
                    </div>
                </div>

                <!-- 表格内容 -->
                <div class="table-container">
                    <table class="data-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>名称</th>
                                <th>分类</th>
                                <th>描述</th>
                                <th>创建时间</th>
                                <th>状态</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr v-if="loading">
                                <td colspan="6" class="loading-cell">🔄 数据加载中...</td>
                            </tr>
                            <tr v-else-if="!tableData.length">
                                <td colspan="6" class="empty-cell">📭 暂无数据，请选择筛选条件</td>
                            </tr>
                            <tr v-else v-for="item in tableData" :key="item.id">
                                <td>{{ item.id }}</td>
                                <td>{{ item.name }}</td>
                                <td>{{ item.category }}</td>
                                <td>{{ item.description }}</td>
                                <td>{{ item.createTime }}</td>
                                <td>
                                    <span :class="['status-tag', item.status]">
                                        {{ item.statusText }}
                                    </span>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- 操作日志 -->
            <div class="log-section">
                <h2>📝 操作日志</h2>
                <div class="log-container">
                    <div v-for="(log, index) in operationLogs" :key="index" class="log-item">
                        <span class="log-time">{{ log.time }}</span>
                        <span class="log-message">{{ log.message }}</span>
                    </div>
                    <div v-if="!operationLogs.length" class="empty-log">
                        暂无操作记录
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        const { createApp, ref, computed } = Vue

        // 级联选择器组件
        const CascaderSelect = {
            props: {
                options: {
                    type: Array,
                    required: true
                },
                modelValue: {
                    type: Array,
                    default: () => []
                },
                placeholder: {
                    type: String,
                    default: '请选择'
                }
            },
            emits: ['update:modelValue', 'change'],
            setup(props, { emit }) {
                const showDropdown = ref(false)
                const tempSelectedPath = ref([...props.modelValue])
                
                const cascaderPanels = computed(() => {
                    const panels = [props.options]
                    
                    let currentOptions = props.options
                    for (let i = 0; i < tempSelectedPath.value.length; i++) {
                        const selectedValue = tempSelectedPath.value[i]
                        const selectedOption = currentOptions.find(option => option.name === selectedValue)
                        
                        if (selectedOption && selectedOption.children) {
                            panels.push(selectedOption.children)
                            currentOptions = selectedOption.children
                        } else {
                            break
                        }
                    }
                    
                    return panels
                })
                
                const displayValue = computed(() => {
                    if (!props.modelValue.length) return ''
                    
                    let currentOptions = props.options
                    let finalLabel = ''
                    
                    for (const value of props.modelValue) {
                        const option = currentOptions.find(opt => opt.name === value)
                        if (option) {
                            finalLabel = option.nameCn
                            currentOptions = option.children || []
                        }
                    }
                    
                    return finalLabel
                })
                
                const toggleDropdown = () => {
                    showDropdown.value = !showDropdown.value
                    if (showDropdown.value) {
                        tempSelectedPath.value = [...props.modelValue]
                    }
                }
                
                const isOptionActive = (option, level) => {
                    return tempSelectedPath.value[level] === option.name
                }
                
                const handleOptionClick = (option, level) => {
                    const newPath = tempSelectedPath.value.slice(0, level)
                    newPath.push(option.name)
                    tempSelectedPath.value = newPath
                }
                
                const handleConfirm = () => {
                    const selectedOptions = []
                    let currentOptions = props.options
                    
                    for (const value of tempSelectedPath.value) {
                        const option = currentOptions.find(opt => opt.name === value)
                        if (option) {
                            selectedOptions.push(option)
                            currentOptions = option.children || []
                        }
                    }
                    
                    emit('update:modelValue', [...tempSelectedPath.value])
                    emit('change', [...tempSelectedPath.value], selectedOptions)
                    
                    showDropdown.value = false
                }
                
                const handleCancel = () => {
                    tempSelectedPath.value = [...props.modelValue]
                    showDropdown.value = false
                }
                
                const getHeaderLabel = (level) => {
                    const labels = ['分类', '子分类', '组件', '详细', '子项']
                    return labels[level] || `级别 ${level + 1}`
                }
                
                return {
                    showDropdown,
                    tempSelectedPath,
                    cascaderPanels,
                    displayValue,
                    toggleDropdown,
                    isOptionActive,
                    handleOptionClick,
                    handleConfirm,
                    handleCancel,
                    getHeaderLabel
                }
            },
            template: `
                <div class="cascader-select">
                    <div class="input-wrapper" @click="toggleDropdown">
                        <input
                            :value="displayValue"
                            :placeholder="placeholder"
                            readonly
                            class="cascader-input"
                        />
                        <span 
                            class="arrow-icon"
                            :class="{ 'arrow-up': showDropdown }"
                        >
                            ▼
                        </span>
                    </div>

                    <div 
                        v-show="showDropdown" 
                        class="cascader-dropdown"
                    >
                        <div class="cascader-content">
                            <div class="cascader-header">
                                <div 
                                    v-for="(panel, level) in cascaderPanels"
                                    :key="'header-' + level"
                                    class="cascader-header-item"
                                >
                                    {{ getHeaderLabel(level) }}
                                </div>
                            </div>
                            <div class="cascader-panels">
                                <div
                                    v-for="(panel, level) in cascaderPanels"
                                    :key="level"
                                    class="cascader-panel"
                                >
                                    <div
                                        v-for="option in panel"
                                        :key="option.name"
                                        class="cascader-option"
                                        :class="{
                                            'is-active': isOptionActive(option, level),
                                            'has-children': option.children && option.children.length > 0
                                        }"
                                        @click="handleOptionClick(option, level)"
                                    >
                                        <span class="option-label">{{ option.nameCn }}</span>
                                        <span 
                                            v-if="option.children && option.children.length > 0"
                                            class="option-arrow"
                                        >
                                            ＞
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="cascader-footer">
                            <button 
                                class="btn btn-cancel"
                                @click="handleCancel"
                            >
                                取消
                            </button>
                            <button 
                                class="btn btn-confirm"
                                @click="handleConfirm"
                                :disabled="!tempSelectedPath.length"
                            >
                                确定
                            </button>
                        </div>
                    </div>
                </div>
            `
        }

        // 主应用
        createApp({
            components: {
                CascaderSelect
            },
            setup() {
                // 级联选择器数据
                const options = [
                    {
                        name: 'component',
                        nameCn: '组件',
                        nameEn: 'Component',
                        children: [
                            {
                                name: 'basic',
                                nameCn: '基础组件',
                                nameEn: 'Basic',
                                children: [
                                    { name: 'button', nameCn: '按钮', nameEn: 'Button' },
                                    { name: 'input', nameCn: '输入框', nameEn: 'Input' },
                                    { name: 'icon', nameCn: '图标', nameEn: 'Icon' },
                                ]
                            },
                            {
                                name: 'form',
                                nameCn: '表单组件',
                                nameEn: 'Form',
                                children: [
                                    { name: 'select', nameCn: '选择器', nameEn: 'Select' },
                                    { name: 'cascader', nameCn: '级联选择器', nameEn: 'Cascader' },
                                    { name: 'switch', nameCn: '开关', nameEn: 'Switch' },
                                ]
                            },
                            {
                                name: 'data',
                                nameCn: '数据展示',
                                nameEn: 'Data',
                                children: [
                                    { name: 'table', nameCn: '表格', nameEn: 'Table' },
                                    { name: 'pagination', nameCn: '分页', nameEn: 'Pagination' },
                                    { name: 'tree', nameCn: '树形控件', nameEn: 'Tree' },
                                ]
                            }
                        ]
                    },
                    {
                        name: 'guide',
                        nameCn: '指南',
                        nameEn: 'Guide',
                        children: [
                            {
                                name: 'design',
                                nameCn: '设计原则',
                                nameEn: 'Design',
                                children: [
                                    { name: 'consistency', nameCn: '一致性', nameEn: 'Consistency' },
                                    { name: 'feedback', nameCn: '反馈', nameEn: 'Feedback' },
                                ]
                            }
                        ]
                    }
                ]

                // 响应式数据
                const selectedValue = ref([])
                const selectedLabels = ref([])
                const tableData = ref([])
                const loading = ref(false)
                const operationLogs = ref([])

                // 计算属性 - 请求参数（最深层级的name值）
                const requestParam = computed(() => {
                    if (!selectedValue.value.length) return ''
                    return selectedValue.value[selectedValue.value.length - 1]
                })

                // 方法 - 更新选中值
                const updateSelectedValue = (value) => {
                    selectedValue.value = value
                }

                // 方法 - 处理级联选择器变更
                const handleCascaderChange = (value, selectedOptions) => {
                    selectedLabels.value = selectedOptions.map(opt => opt.nameCn)
                    
                    // 添加操作日志
                    addLog(`级联选择器值变更: ${selectedLabels.value.join(' → ')}`)
                    addLog(`提取请求参数: ${requestParam.value}`)
                    
                    // 触发表格数据请求
                    if (requestParam.value) {
                        fetchTableData(requestParam.value)
                    } else {
                        tableData.value = []
                    }
                }

                // 方法 - 获取请求参数对象
                const getRequestParams = () => {
                    const params = {
                        category: requestParam.value || null,
                        page: 1,
                        pageSize: 10
                    }
                    return JSON.stringify(params, null, 2)
                }

                // 方法 - 模拟表格数据请求
                const fetchTableData = async (category) => {
                    loading.value = true
                    addLog(`开始请求表格数据，参数: category=${category}`)
                    
                    try {
                        // 模拟API请求延迟
                        await new Promise(resolve => setTimeout(resolve, 1000))
                        
                        // 模拟不同分类的数据
                        const mockData = generateMockData(category)
                        tableData.value = mockData
                        
                        addLog(`请求完成，获取到 ${mockData.length} 条数据`)
                    } catch (error) {
                        addLog(`请求失败: ${error}`)
                        tableData.value = []
                    } finally {
                        loading.value = false
                    }
                }

                // 方法 - 生成模拟数据
                const generateMockData = (category) => {
                    const dataMap = {
                        'button': [
                            { id: 1, name: '主要按钮', category: '按钮', description: '用于主要操作', createTime: '2024-01-01', status: 'active', statusText: '启用' },
                            { id: 2, name: '次要按钮', category: '按钮', description: '用于次要操作', createTime: '2024-01-02', status: 'inactive', statusText: '禁用' },
                            { id: 3, name: '危险按钮', category: '按钮', description: '用于危险操作', createTime: '2024-01-03', status: 'active', statusText: '启用' }
                        ],
                        'input': [
                            { id: 4, name: '文本输入框', category: '输入框', description: '基础文本输入', createTime: '2024-01-04', status: 'active', statusText: '启用' },
                            { id: 5, name: '数字输入框', category: '输入框', description: '数字专用输入', createTime: '2024-01-05', status: 'active', statusText: '启用' }
                        ],
                        'table': [
                            { id: 6, name: '基础表格', category: '表格', description: '显示表格数据', createTime: '2024-01-06', status: 'active', statusText: '启用' },
                            { id: 7, name: '可选择表格', category: '表格', description: '支持行选择', createTime: '2024-01-07', status: 'active', statusText: '启用' },
                            { id: 8, name: '可编辑表格', category: '表格', description: '支持行内编辑', createTime: '2024-01-08', status: 'inactive', statusText: '禁用' }
                        ],
                        'select': [
                            { id: 9, name: '单选选择器', category: '选择器', description: '单选下拉框', createTime: '2024-01-09', status: 'active', statusText: '启用' },
                            { id: 10, name: '多选选择器', category: '选择器', description: '多选下拉框', createTime: '2024-01-10', status: 'active', statusText: '启用' }
                        ]
                    }
                    
                    return dataMap[category] || [
                        { id: 999, name: `${category} 示例数据`, category: category, description: '这是模拟数据', createTime: '2024-01-01', status: 'active', statusText: '启用' }
                    ]
                }

                // 方法 - 添加操作日志
                const addLog = (message) => {
                    const now = new Date()
                    const time = now.toLocaleTimeString()
                    operationLogs.value.unshift({ time, message })
                    
                    // 只保留最近20条日志
                    if (operationLogs.value.length > 20) {
                        operationLogs.value = operationLogs.value.slice(0, 20)
                    }
                }

                return {
                    options,
                    selectedValue,
                    selectedLabels,
                    tableData,
                    loading,
                    operationLogs,
                    requestParam,
                    updateSelectedValue,
                    handleCascaderChange,
                    getRequestParams
                }
            }
        }).mount('#app')
    </script>
</body>
</html>
